package com.song.dao.impl;


import com.song.dao.AdministratorDao;
import com.song.exception.adminException.AdminRegisterNameException;
import com.song.exception.adminException.AdminRegisterPhoneException;
import com.song.polo.Administrator;
import com.song.utils.TemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

@Repository
public class AdministratorDaoImpl implements AdministratorDao{
    /**
     * 登录的sql语句
     */
    private static final String LOGIN_SQL = "SELECT * FROM administrator WHERE administratorName = ? AND password = ?";
    /**
     * 登录后更新登录时间与登录IP地址sql
     */
    private static final String UPDATELOGINTIME_SQL = "UPDATE administrator SET loginTime = ?,ipAddress = ? WHERE administratorName = ?";
    /**
     * 注册的sql
     */
    private static final String INSERT_SQL = "INSERT INTO administrator VALUES(NULL,?,?,?,null,NULL,?)";
    /**
     * 通过手机号或管理员名查询的sql
     */
    private static final String SELECTBYPHONE_SQL = "SELECT * FROM administrator WHERE";
    @Autowired
    private JdbcTemplate template;
    @Autowired
    private Administrator administrator;


    /**
     * 管理员登录
     *
     * @param administratorName 管理员名管理员密码
     * @return true 登录成功
     */
    @Override
    public Administrator login(String administratorName, String password){
        try {

            return template.queryForObject(LOGIN_SQL, new Object[]{administratorName, password}, new RowMapper<Administrator>() {
                @Override
                public Administrator mapRow(ResultSet resultSet, int i) throws SQLException {
                    return TemplateUtil.getObject(resultSet);
                }
            });
        } catch (Exception e) {

            return null;
        }
    }





    @Override
    public void updateLoginTime(String administratorName, String ipAddress, Timestamp loginTime) {
        template.update(UPDATELOGINTIME_SQL, loginTime, ipAddress, administratorName);

    }



    @Override
    public Integer register(Administrator administrator) throws AdminRegisterNameException, AdminRegisterPhoneException {
        int update = 0;
        try {
            update = template.update(INSERT_SQL, administrator.getAdministratorName(),
                    administrator.getAdministratorPhone(),administrator.getPassword(),administrator.getRegisterTime());
        } catch (Exception e) {
            if (e.getMessage().endsWith("key 'administrator.administratorName'")){
                throw new AdminRegisterNameException(e.getMessage());
            }
            if (e.getMessage().endsWith("key 'administrator.administratorPhone'")){
                throw new AdminRegisterPhoneException(e.getMessage());
            }
        }
        if (update > 0){
            return 1;
        }
        return -2;
    }



    @Override
    public Administrator selectByPhone(String administratorPhone, String administratorName) {
        String sql = null;
        String str = null;
        try {
            if (administratorName != null && !"".equals(administratorName)) {
                sql = SELECTBYPHONE_SQL + " administratorName = ?";
                str = administratorName;
            } else if (administratorPhone != null && !"".equals(administratorPhone)) {
                sql = SELECTBYPHONE_SQL + " administratorPhone = ?";
                str = administratorPhone;
            }
            return template.queryForObject(sql, new Object[]{str}, new RowMapper<Administrator>() {
                @Override
                public Administrator mapRow(ResultSet resultSet, int i) throws SQLException {
                    return TemplateUtil.getObject(resultSet);
                }
            });
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
